package com.atguigu.cloud.exp;

import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {//controller层代码抛出的异常都会被这个全局异常类捕获
    @ExceptionHandler(RuntimeException.class)//标注需要处理的异常类型，运行异常
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)//HttpStatus是一个官方枚举类，INTERNAL_SERVER_ERROR是里面的状态码
    public ResultData<String> exception(Exception e){
        System.out.println("#####come in GlobalExceptionHandler");
        log.error("全局异常信息{}",e.getMessage());
        System.out.println(e.getMessage());
        return ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());

    }

}
